/**
 * Created by Administrator on 2017/1/16.
 */

var MJPlayer = require('../MJ/player').MJPlayer;
var MJ = require('../MJ/MJ');
var Const = require('./const');
var SubCode = require('../subCode');

exports.HFMJPlayer = MJPlayer.extend({
   // _selectPiao: 0,
    _yao: 0,
    _dabao: 0,
    baoCardScore: 0,
    _isBaoHu: 0,
    _PassPengCard: 0,
    init: function(uid, msg) {
        this._super(uid, msg);

        this._stat.zmCount = 0;             // 自摸次数
        this._stat.dpCount = 0;             // 点炮次数
        this._stat.dhCount = 0;             // 点胡次数
        this._stat.anGangCount = 0;        // 暗杠次数
        this._stat.mingGangCount = 0;      // 明杠次数
        this._stat.lzCount = 0;             // 庄家次数
        this._stat.roundRules = {};
        this._yao = 0;                      //摇骰子
        this._dabao = 0;                    //打宝
        this.baoCardScore = 0;              //宝牌分
        this._isBaoHu = false;
        this._PassPengCard = 0;
       // this._stat.roundPiaoScore = 0;     // 飘分

     //   this._selectPiao = -1;               // 选择飘 -1/0/1 未选择/不飘/飘
    },

   /* isPiao: function() {
        return this._selectPiao == 1;
    },*/

   /* setPiao: function(piao) {
        this._selectPiao = (piao) ? 1 : 0;
    },*/

  /*  isPiaoSelected: function() {
        return this._selectPiao != -1;
    },*/

    settlementGangScore: function() {
        var player = this;
        var room = this.getRoom();

        if (!this.isRoundTing()) {
            return;
        }

        var totalGangScore = 0;
        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.overdue || gangInfo.unscored) {
                continue;
            }

            if (gangInfo.type) {
                var score = Const.SpecGangScore[gangInfo.type] || 0;
                if (gangInfo.buCards) {
                    score += gangInfo.buCards.length * 2;
                }
                totalGangScore += score;
            } else {
                var score = Const.GangScore[gangInfo.gang] || 0;
                if (gangInfo.card == MJ.Value.TONG1 || gangInfo.card == MJ.Value.TIAO1 || gangInfo.card == MJ.Value.WAN1) {
                    score *= 2;
                }
                totalGangScore += score;
            }
        }

        if (player.getRoundResult() == MJ.Result.ZM && room.getOption("XFG")) {
            var huCard = player.getRoundHuCard();
            if (huCard == MJ.ZI.HZ || huCard == MJ.ZI.FC || huCard == MJ.ZI.BB) {
                if (this._isHuZFB(player.getRoundHuCards())) {
                    totalGangScore += 2;
                }
            }
        }

        room.enumPlayers(function(eIndex, ePlayer) {
            if (eIndex != player.getIndex()) {
                player.addRoundGangScore(totalGangScore);
                ePlayer.addRoundGangScore(-totalGangScore);
            }
        });
    },
    settlementBaocardScore: function(){
        var room = this.getRoom();
        var player = this;
        if(room.getBaoCardNum(this) == 4){
            room.enumPlayers(function(eIndex, ePlayer) {
                if (eIndex == player.getIndex()) {
                    return;
                }
                ePlayer.addRoundBaoScore(-10);
                ePlayer.addRoundScore(-10);
                player.addRoundScore(10);
                player.addRoundBaoScore(10);
            });
        }
    },
    settlementPatternScore: function() {
        if (!this.isRoundHu()) {
            return;
        }

        var player = this;
        var room = this.getRoom();
        //每家都有
        var all = 0;
        ExObject.each(this._stat.roundRules, function(rule, value) {
            player.addRoundItemScores(rule, Const.ItemScore[rule]);
            player.setRoundScoreRule(rule);
        });
        ExObject.eachKeyNum(this._stat.roundScoreRules, function(rule, value) {
            if(rule == Const.ScoreRule.PPH ||
                rule == Const.ScoreRule.PPHHYS ||
                rule == Const.ScoreRule.QYSGSH ||
                rule == Const.ScoreRule.QYSQG ||
                rule == Const.ScoreRule.HYSGSH ||
                rule == Const.ScoreRule.HYSQG ||
                rule == Const.ScoreRule.QZQQG ||
                rule == Const.ScoreRule.ONE13QG ||
                rule == Const.ScoreRule.PPHQYS
            ){
                all += Const.ItemScore[rule];
            }
        });
        var isDPQB = room.getOption("DPQB");
        var dpPlayer = room.getPlayerByIndex(room.getFirstDPIndex());
        var ItemScore = player.getRoundItemScore();
        ItemScore -= all;
        //计算玩家分数
        if(this.isBaohu()){
            this._stat.roundPatternScore /= 2;
            ItemScore /= 2;
            all /= 2;
        }
        if(player == room.getDealerPlayer()){
            if(this._stat.roundPattern != MJ.Pattern.PAIR7 && player._stat.roundPattern != Const.Pattern.PPH){
                this._stat.roundPatternScore *= 2;
            }
        }

        // 结算牌型及基础分


        room.enumPlayers(function(eIndex, ePlayer) {
            if (eIndex == player.getIndex()) {
                return;
            }
            var score = 0;

            var playerbaoScore = player._dabao * 10;
            var curPlayer = room.getPlayerByIndex(eIndex);
            var curplayerbaoScore = curPlayer._dabao * 10;
            if(player._stat.roundPattern == Const.Pattern.PAIR7 || player._stat.roundPattern == Const.Pattern.SIBAO || player._stat.roundPattern == Const.Pattern.PPH){
                ePlayer.addRoundScore(-player._stat.roundPatternScore);
                player.addRoundScore(player._stat.roundPatternScore);

            }else {
                if(player._stat.roundPattern == Const.Pattern.NORMAL){
                    if(ItemScore == 0 && all == 0){
                        if(curPlayer == room.getDealerPlayer() ){
                            score += player._stat.roundPatternScore;
                        }
                        score += player._stat.roundPatternScore;
                    }
                }else{
                    if(curPlayer == room.getDealerPlayer() ){
                        score += player._stat.roundPatternScore;
                    }
                    score += player._stat.roundPatternScore;
                }
            }
            var curbaoScore = playerbaoScore + curplayerbaoScore;
            score += curbaoScore;
            score += all;
            if(isDPQB || player._stat.roundResult == MJ.Result.ZM){// 三家出
                score += ItemScore;
                ePlayer.addRoundScore(-score);
                ePlayer.addRoundBaoScore(-curbaoScore);
                player.addRoundScore(score);
                player.addRoundBaoScore(curbaoScore);
            }else{
                if(dpPlayer && dpPlayer == ePlayer){
                    score += ItemScore;
                    player.addRoundScore(score);
                    player.addRoundBaoScore(curbaoScore);
                    dpPlayer.addRoundScore(-score);
                    dpPlayer.addRoundBaoScore(-curbaoScore);
                }else {
                    ePlayer.addRoundScore(-all);
                    player.addRoundScore(all);
                    /*ePlayer.addRoundScore(-curbaoScore);
                    ePlayer.addRoundBaoScore(-curbaoScore);
                    player.addRoundScore(curbaoScore);
                    player.addRoundBaoScore(curbaoScore);*/
                }
            }
        });
       /* var piaoScore = room.getOption("PIAO");*/
    },
    addRoundBaoScore: function(score) {
        this.baoCardScore += score;
    },
    /**
     * 结算点炮全包
     * @param dpPlayer
     */


    settlementRules: function() {
        var rules = this._stat.roundRules;
        var room = this.getRoom();
        var player = this;

        // 判断门清
        /*if (this._chiCards.length == 0 && this._pengCards.length == 0 && !this._existKaiMenGang()) {
            rules[Const.ScoreRule.MenQing] = 1;
        }*/


        if (!this.isRoundHu()) {
            return;
        }
        //var baocard = room.getBaoCardNum(this);
        // 判断 清一色/混一色
        var colors = {};
        var existSpecGang = false;
        var existAnGang = false;
        if(this.isBaohu()){
            ExObject.eachKeyNum(this._handCards.getCards(), function(card, num) {
                if(card != room.baoCard){
                    colors[MJ.getColor(card)] = 1;
                }
            });
        }else{
            ExObject.eachKeyNum(this._handCards.getCards(), function(card, num) {
                colors[MJ.getColor(card)] = 1;
            });

        }
        if(this._stat.huCard != room.baoCard)
            colors[MJ.getColor(this._stat.huCard)] = 1;

        this._pengCards.forEach(function (card) {
            colors[MJ.getColor(card)] = 1;
        });

        this._chiCards.forEach(function(chiInfo) {
            colors[MJ.getColor(chiInfo.card)] = 1;
        });

        this._gangCards.forEach(function (gangInfo) {
            if (gangInfo.type) {
                existSpecGang = true;
            } else {
                if (gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                    existAnGang = true;
                }
                colors[MJ.getColor(gangInfo.card)] = 1;
            }
        });

        var existColorZI = colors.hasOwnProperty(MJ.Color.ZI);

        delete colors[MJ.Color.ZI];
        var colorNum = Object.keys(colors).length;

        if (colorNum == 1) {
            if (existSpecGang || existColorZI) {
                if(this._stat.roundPattern == Const.Pattern.PPHGSH){
                    rules[Const.ScoreRule.PPHGSHHYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.PPH){
                    rules[Const.ScoreRule.PPHHYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.GANGKAI){
                    rules[Const.ScoreRule.HYSGSH] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.TIANHU){
                    rules[Const.ScoreRule.THHYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.DIHU){
                    rules[Const.ScoreRule.DHHYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.QIANGGANG){
                    rules[Const.ScoreRule.HYSQG] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.NORMAL){
                    var pattern = Const.Pattern.HUNYISE;
                    this.setRoundPattern(pattern, Const.PatternScore[pattern]);
                }else{
                    rules[Const.ScoreRule.HunYiSe] = 1;

                }
            } else {
                if(this._stat.roundPattern == Const.Pattern.PPHGSH){
                    rules[Const.ScoreRule.PPHGSHQYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.PPH){
                    rules[Const.ScoreRule.PPHQYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.GANGKAI){
                    rules[Const.ScoreRule.QYSGSH] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.TIANHU){
                    rules[Const.ScoreRule.THQYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.DIHU){
                    rules[Const.ScoreRule.DHQYS] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.QIANGGANG){
                    rules[Const.ScoreRule.QYSQG] = 1;
                }else if(this._stat.roundPattern == Const.Pattern.NORMAL){
                    var pattern = Const.Pattern.QINGYISE;
                    this.setRoundPattern(pattern, Const.PatternScore[pattern]);
                }else{
                    rules[Const.ScoreRule.QingYiSe] = 1;
                }
            }
        }else if(colorNum == 0){
            if(this._stat.roundPattern == Const.Pattern.PPHGSH){
                rules[Const.ScoreRule.PPHGSHQYS] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.PPH){
                rules[Const.ScoreRule.PPHQYS] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.GANGKAI){
                rules[Const.ScoreRule.QYSGSH] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.TIANHU){
                rules[Const.ScoreRule.THQYS] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.DIHU){
                rules[Const.ScoreRule.DHQYS] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.QIANGGANG){
                rules[Const.ScoreRule.QYSQG] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.NORMAL){
                var pattern = Const.Pattern.QINGYISE;
                this.setRoundPattern(pattern, Const.PatternScore[pattern]);
            }else{
                rules[Const.ScoreRule.QingYiSe] = 1;
            }
        }else{
            if(this._stat.roundPattern == Const.Pattern.PPHGSH){
                rules[Const.ScoreRule.GSH] = 1;
                this.setRoundPattern(Const.Pattern.PPH, Const.ItemScore[Const.ScoreRule.PPH]);
                //this._stat.roundPatternScore = 20;
            }else if(this._stat.roundPattern == Const.Pattern.PPH){
                this._stat.roundPatternScore = 20;
            }else if(this._stat.roundPattern == Const.Pattern.GANGKAI){
                rules[Const.ScoreRule.GSH] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.QIANGGANG){
                rules[Const.ScoreRule.QG] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.TIANHU){
                rules[Const.ScoreRule.TH] = 1;
            }else if(this._stat.roundPattern == Const.Pattern.DIHU){
                rules[Const.ScoreRule.DIHU] = 1;
            }
        }

        if (this.getRoundPattern() != Const.Pattern.NORMAL) { // 七对/13 不可能有后面的规则
            return;
        }

    },
    addDaBao:function (num){
        this._dabao += num;
    },
    settlement: function() {
        //this.addRoundScore(this._stat.roundGangScore);
        //this.addRoundScore(this._stat.roundPiaoScore);

        if (this._stat.roundResult == MJ.Result.ZM) {
            this._stat.zmCount++;
        } else if (this._stat.roundResult == MJ.Result.DH) {
            this._stat.dhCount++;
            if (this._stat.roundTarget.length > 0) {
                var dpPlayer = this.getRoom().getPlayerByIndex(this._stat.roundTarget[0]);
                if (dpPlayer) {
                    dpPlayer._stat.dpCount++;
                }
            }
        }

        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.type) {

            } else {
                if (gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                    this._stat.anGangCount++;
                } else {
                    this._stat.mingGangCount++;
                }
            }
        }
        if (this.isDealer()) {
            this._stat.lzCount++;
        }
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/
    //断线重连信息
    getReconnectInfo: function(isSelf) {
        var info = this._super(isSelf);
        info.remainScore = this._stat.score;
        return info;
    },

    getSettlementInfo: function() {
        var info = this._super();
        info.gangCards = this._gangCards;
        info.pengCards = this._pengCards;
        info.chiCards = this._chiCards;
        info.handCards = this._handCards.getCards();
        info.baoCardScore = this.baoCardScore;
        info.remainScore = this._stat.score;
        info.isBaoHu = this._isBaoHu;
        return info;
    },

    onStartNewRound: function() {
        this._super();
        //this._selectPiao = -1;
        this._stat.roundRules = {};
        this._dabao = 0;
        this.baoCardScore = 0;
        //this._stat.roundPiaoScore = 0;
    },

    getInitHandCardPreCards: function() {
        var preCards = [];
        var room = this.getRoom();

        var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
        if (distance == 0) {
            /*preCards.push(5);
            preCards.push(5);
            preCards.push(5);

            preCards.push(13);
            preCards.push(13);
            preCards.push(13);
            preCards.push(13);

            preCards.push(25);
            preCards.push(25);
            preCards.push(25);

            preCards.push(33);
            preCards.push(33);
            preCards.push(33);*/
        } else if (distance == 1) {

        }

        return preCards;
    },

    gangCard: function(gangTask) {
        var selectGang = gangTask.selectGang || 0;
        if (selectGang > gangTask.gangArray.length) {
            selectGang = 0;
        }

        var selectGangTask = gangTask.gangArray[selectGang];

        if (selectGangTask.type) {
            return this._specGangCard(selectGangTask);
        } else {
            return this._super(selectGangTask);
        }
    },

    _getSpecGang: function(type) {
        var result = null;
        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.type == type) {
                result = gangInfo;
                break;
            }
        }
        return result;
    },

    _specGangCard: function(gangTask) {
        var gangInfo = null;
        var self = this;

        if (gangTask.buCard) {
            gangInfo = this._getSpecGang(gangTask.type);
            if (gangInfo) {
                if (!gangInfo.buCards) {
                    gangInfo.buCards = [];
                }

                var buCard = gangTask.buCard;
                if (this._handCards.getCardNum(buCard) > 0) {
                    this._handCards.delCard(buCard);
                    gangInfo.buCards.push(buCard);
                }

                gangInfo.buCard = gangTask.buCard;
            }
        } else if (gangTask.cards) {
            gangTask.cards.forEach(function(card) {
                this._handCards.delCard(card);
            }.bind(this));

            gangInfo = clone(gangTask);
            delete gangInfo.playerIndex;
            this._gangCards.push(gangInfo);
        }

        return gangInfo;
    },

    cancelLastGangForQG: function(card) {
        var gangIndex = this._gangCards.length - 1;
        if (gangIndex < 0) {
            return;
        }

        var gangInfo = this._gangCards[gangIndex];
        if (gangInfo.type) {
            if (gangInfo.buCard != card) {
                return;
            }

            if (gangInfo.buCards.length < 1) {
                return;
            }

            gangInfo.buCards.splice(gangInfo.buCards.length - 1, 1);
            gangInfo.buCard = (gangInfo.buCards.length > 0) ? gangInfo[gangInfo.buCards.length - 1] : 0;
        } else {
            return this._super(card);
        }
    },

    checkChiCondition: function(card) {
        var subConfig = this.getRoom().getSubConfig();
        var room = this.getRoom();
        var color = MJ.getColor(card);
        if (subConfig.colors.indexOf(color) == -1) {
            return false;
        }

        /*if (!MJ.isBasicColor(color)) {
            return false;
        }*/
        /*if(card == room.baoCard) {
            return false;
        }*/
        return true;
    },

    /*checkHuLogic: function(cards) {
        var pattern = MJ.Pattern.NORMAL;
        pattern = this.checkBaoHuLogic(cards);
        return pattern;
    },*/

    extendPattern: function(cards, pattern, card, colors) {
        if (pattern == MJ.Pattern.NONE) {
            return pattern;
        }

        if (pattern != Const.Pattern.PAIR7 && pattern != Const.Pattern.ONE13) {
            pattern = MJ.Pattern.NORMAL;
        }

        return pattern;
    },

    getColorsWithCards: function(cards) {
        var colors = {};

        ExObject.eachKeyNum(cards, function(card, num) {
            colors[MJ.getColor(card)] = 1;
        });

        this._pengCards.forEach(function (card) {
            colors[MJ.getColor(card)] = 1;
        });

        this._gangCards.forEach(function (gangInfo) {
            if (gangInfo.type || gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                // 不算开门
            } else {
                colors[MJ.getColor(gangInfo.card)] = 1;
            }
        });

        delete colors[MJ.Color.ZI];

        return ExObject.numberKeys(colors);
    },

    _existKaiMenGang: function() {
        var exist = false;
        this._gangCards.forEach(function (gangInfo) {
            if (gangInfo.type || gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                // 不算开门
            } else {
                exist = true;
            }
        });
        return exist;
    },

    /**
     * 是否中发白可以组成胡牌
     * @param cards
     * @returns {boolean}
     * @private
     */
    _isHuZFB: function(cards) {
        var zfbCards = {};
        var zfbCount = 0;
        var zfbArr = [MJ.ZI.HZ, MJ.ZI.FC, MJ.ZI.BB];
        zfbArr.forEach(function(card) {
            if (cards[card] > 0) {
                zfbCards[card] = cards[card];
                zfbCount++;
            }
        });

        if (zfbCount != 3) {
            return false;
        }

        var huZFB = false;
        var zfbSplits = MJ.splitColorCards(MJ.ZI.HZ, MJ.ZI.BB, zfbCards);
        zfbSplits.forEach(function(item) {
            if (ExNumber.isSameArray(item, zfbArr)) {
                huZFB = true;
            }
        })

        return huZFB;
    },
    //检测碰牌
    checkPengWithCard: function(card) {

        if (this.getRoom().isSelectColor()) {
            if (MJ.getColor(card) == this.getSelectColor()) {
                return false;
            }
        }
        var room = this.getRoom();

        return this._handCards.getCardNum(card) >= 2;
    },
    //检测杠牌
    checkGangWithCard: function(card) {
        if (this.getRoom().isSelectColor()) {
            if (MJ.getColor(card) == this.getSelectColor()) {
                return false;
            }
        }
        var room = this.getRoom();
        return (this._handCards.getCardNum(card) == 3);
    },
    //吃牌
    chiCard: function(chiTask) {
        var card = chiTask.card;
        var selectCard = chiTask.selectCard;
        if (!selectCard || !(chiTask.maxCards[selectCard])) {
            selectCard = 0;
            chiTask.selectCard = selectCard;
        }

        /*if (MJ.getPoint(selectCard) < 3) {
            return false;
        }

        var exist = true;
        for (var i = 0; i < 3; ++i) {
            var tmpCard = selectCard - i;
            if (tmpCard != card && this._handCards.getCardNum(tmpCard) < 1) {
                exist = false; break;
            }
        }

        if (!exist) {
            return false;
        }
        */
        var tmpCards = chiTask.maxCards[selectCard];
        for (var i = 0; i < 3; ++i) {
            var tmpCard = tmpCards[i];
            if (tmpCard != card) {
                this._handCards.delCard(tmpCard);
            }
        }

        var chiInfo = clone(chiTask);
        delete chiInfo.maxCards;
        chiInfo.selectCard = tmpCards;
        this._chiCards.push(chiInfo);

        return true;
    },
    //检测吃牌
    checkChiWithCard: function(card) {
        if (!this.checkChiCondition(card)) {
            return null;
        }
        var room = this.getRoom();
        var color = MJ.getColor(card);
        var point = MJ.getPoint(card);
        var maxCards = [];
        if (MJ.isBasicColor(color)) {
            var maxPoint = MJ.ColorCardNum[color].point;
            for (var p = 0; p < 3; ++p) {
                var tmpPoint = point + p;
                if (tmpPoint >= 3 && tmpPoint <= maxPoint) {
                    var exist = true;
                    for (var i = 0; i < 3; ++i) {
                        var tmpCard = MJ.getValue(color, tmpPoint - i);
                        if (tmpCard != card && this._handCards.getCardNum(tmpCard) < 1) {
                            exist = false;
                            break;
                        }/*if (tmpCard == room.baoCard) {
                            exist = false;
                            break;
                        }*/
                    }
                    if (exist) {
                        var curCards = [];
                        curCards.push(MJ.getValue(color, tmpPoint));
                        curCards.push(MJ.getValue(color, tmpPoint-1));
                        curCards.push(MJ.getValue(color, tmpPoint-2));
                        maxCards.push(curCards);
                    }
                }
            }
        }else{
            if(point > 4){
                for(var i = 5; i <= 7; i++){
                    if(i == point){
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /*if(tmpCard == room.baoCard)
                    {
                        continue
                    }*/
                    var isChi = false;
                    if(this._handCards.getCardNum(tmpCard) > 0){
                        for(var j = 5; j <= 7; j++){
                            if(j == point){
                                continue;
                            }
                            var curCard = MJ.getValue(color, j);
                            /*if(curCard == room.baoCard)
                            {
                                continue;
                            }*/
                            if(curCard == tmpCard)
                            {
                                continue;
                            }
                            if(this._handCards.getCardNum(curCard) > 0){
                                var curCards = [];
                                curCards.push(tmpCard);
                                curCards.push(curCard);
                                curCards.push(card);
                                maxCards.push(curCards);
                                isChi = true;
                                break;
                            }
                        }
                    }
                    if(isChi) {
                        break;
                    }
                }

            }else{
                for(var i = 1; i <= 4; i++){
                    if(i == point){
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /**if(tmpCard == room.baoCard) {
                        continue
                    }*/
                    if(this._handCards.getCardNum(tmpCard) > 0){
                        for(var j = i; j <= 4; j++){
                            if(j == point){
                                continue;
                            }
                            var curCard = MJ.getValue(color, j);
                            /*if(curCard == room.baoCard) {
                                continue;
                            }*/
                            if(curCard == tmpCard) {
                                continue;
                            }
                            if(this._handCards.getCardNum(curCard) > 0){
                                var curCards = [];
                                curCards.push(tmpCard);
                                curCards.push(curCard);
                                curCards.push(card);
                                maxCards.push(curCards);
                            }
                        }
                    }
                }

            }
        }
        return maxCards.length > 0 ? maxCards : null;
    },
    checkHuNLogic: function(cards, huCard) {
        var num4 = 0;
        var num3 = 0;
        var num2 = 0;
        var num = 0;

        ExObject.each(cards, function(c, n) {
            if (n == 2) {
                num2++;
            } else if (n == 3) {
                num3++;
            } else if (n == 4) {
                num4++;
            }
            num += n;
        });

        if (num == 2 && this._chiCards.length <= 0) { // 碰碰胡
            return (num2 == 1) ? Const.Pattern.PPH : MJ.Pattern.NONE;
        }
        var newCards = clone(cards);
        if (this.check13BuDa(newCards,this._isBaoHu)) {//十三不搭
            var isQzq = true;
            newCards = clone(cards);
            var colorCards = MJ.getColorCards(newCards, MJ.Color.ZI);

            var colorCardsNum = MJ.sumCardsNum(colorCards);
            var room = this.getRoom();
            if (colorCardsNum == 0) {
                isQzq = false;
            }
            for (var i = MJ.ZI.DF; i <= MJ.ZI.BB; ++i) {
                var num = colorCards[i];
                if (!num) {
                    isQzq = false;
                    break;
                }
            }
            if (isQzq) {
                return Const.Pattern.QZQ;
            }
            return Const.Pattern.ONE13;
        }
        if ((num2 + num4*2) == 7) { // 七对
            return Const.Pattern.PAIR7;
        }
        if (num2 == 1 && ((num3*3 + 2) == num)  && this._chiCards.length <= 0) { // 大对子
            return Const.Pattern.PPH;
        }
        var room = this.getRoom();
        var colors = room.getSubConfig().colors;
        var hu3x2 = false;
        var pattern = MJ.Pattern.NORMAL;
        for (var i = 0, l = colors.length; i < l; ++i) {
            var color = colors[i];
            var colorCards = MJ.getColorCards(cards, color);
            var context = {
                result: false,
                hu3x2:hu3x2,
                curbaocard:0
            };
            if (MJ.isBasicColor(color)) {
                this.checkBaoColorCards(MJ.getValue(color, 1), MJ.getValue(color, MJ.COLOR_CARD_NUM), colorCards, context);
            } else {
                this.checkBaoPairColorCards(colorCards,context)
            }
            if (!context.result) {
                pattern = MJ.Pattern.NONE; break;
            }
            hu3x2 = context.hu3x2;
        }
        return pattern;
    },
    checkHuColorCards: function(minCard, maxCard, colorCards) {
        var colorCardsNum = MJ.sumCardsNum(colorCards);
        if (colorCardsNum == 0) {
            return true;
        }

        var context = {result: false};
        var modNum = colorCardsNum % 3;
        if (modNum == 0) {
            this.checkHu3X(minCard, maxCard, colorCards, context);
        } else if (modNum == 2) {
            this.checkHu3X2(minCard, maxCard, colorCards, context);
        }

        return context.result
    },

    checkHu3X: function(minCard, maxCard, cards, context) {
        if (MJ.sumCardsNum(cards) < 1) {
            context.result = true;
            return;
        }

        for (var i = minCard; i <= maxCard; ++i) {
            var num = cards[i];
            if (!num) {
                continue;
            }

            var num2 = cards[i + 1];
            var num3 = cards[i + 2];

            if (num == 3) {
                if (num2 >= 3 && num3 >= 3) {
                    var newCards = clone(cards);
                    newCards[i] = 0;
                    newCards[i + 1] = num2 - 3;
                    newCards[i + 2] = num3 - 3;
                    this.checkHu3X(minCard, maxCard, newCards, context);
                }

                cards[i] = 0;
                this.checkHu3X(minCard, maxCard, cards, context);
            } else {
                if (num2 && num3) {
                    cards[i] = num - 1;
                    cards[i + 1] = num2 - 1;
                    cards[i + 2] = num3 - 1;
                    this.checkHu3X(minCard, maxCard, cards, context);
                }
            }
            break;
        }
    },

    checkHu3X2: function(minCard, maxCard, cards, context) {
        for (var i = minCard; i <= maxCard; ++i) {
            var num = cards[i];
            if (num >= 2) {
                var newCards = clone(cards);
                newCards[i] = num - 2;
                this.checkHu3X(minCard, maxCard, newCards, context);
                if (context.result) break;
            }
        }
    },

    checkPairHuColorCards: function(cards) {
        var num3 = 0;
        var num2 = 0;
        var numOther = 0;

        ExObject.each(cards, function(c, n) {
            if (n == 2) {
                num2++;
            } else if (n == 3) {
                num3++;
            } else {
                numOther++;
            }
        });

        if((numOther == 0) && (num2 <= 1)) {
            return true;
        }
        var context = {
            result: false,
            hu3x2:false,
            curbaocard:0
        };
        this.checkBaoPairColorCards(cards,context);
        return context.result;
    },
    //宝牌胡牌逻辑
    checkHuLogic:function(cards, huCard) {
        var room = this.getRoom();
        var baocard = cards[room.baoCard];
        if(!baocard){
            baocard = 0;
        }
        var benCard = 0;
        this._isBaoHu = false;
        var pattern = MJ.Pattern.NONE;
        pattern = this.checkHuNLogic(cards, huCard);
        if(pattern != MJ.Pattern.NONE){
            return pattern;
        }
        var player = this;
        if(huCard == room.baoCard){
            if (player.getIndex() != room.getCurPlay()) {
                if (baocard > 1) {
                    baocard -= 1;
                    benCard = 1;
                } else {
                    return pattern;
                }
            }
        }
        var num4 = 0;
        var num3 = 0;
        var num2 = 0;
        var num1 = 0;
        var num = 0;
        var curBaocard = baocard;
        this._isBaoHu = true;
        ExObject.eachKeyNum(cards, function (c, n) {
            if (c != room.baoCard) {
                if (n == 2) {
                    num2++;
                } else if (n == 3) {
                    num3++;
                } else if (n == 4) {
                    num4++;
                }else{
                    num1++;
                }
            }
            num += n;
        });
        /*if (baocard == 4) {//4个宝直接胡牌
            return Const.Pattern.SIBAO;
        }*/
        var newCards = clone(cards);
        if (this.check13BuDa(newCards, this._isBaoHu, benCard)) {//十三不搭
            var isQzq = true;
            newCards = clone(cards);
            var colorCards = MJ.getColorCards(newCards, MJ.Color.ZI);

            var colorCardsNum = MJ.sumCardsNum(colorCards);
            var room = this.getRoom();
            if (colorCardsNum == 0) {
                isQzq = false;
            }
            for (var i = MJ.ZI.DF; i <= MJ.ZI.BB; ++i) {
                var num = colorCards[i];
                if (!num) {
                    isQzq = false;
                    break;
                }
            }
            if (isQzq) {
                return Const.Pattern.QZQ;
            }
            return Const.Pattern.ONE13;
        }
        newCards = clone(cards);
        if (this.checkBaoHuPair7(newCards, baocard)) { // 七对
            return Const.Pattern.BPAIR7;
        }
        newCards = clone(cards);
        if (this.checkBaoHuBigPair(newCards,baocard) && this._chiCards.length <= 0) { //// 碰碰胡
            return Const.Pattern.PPH;
        }
        var colors = room.getSubConfig().colors;


        var hu3x2 = false;
        curBaocard = baocard;
        pattern = MJ.Pattern.NORMAL;

        //遍历每个花色

        for (var i = 0, l = colors.length; i < l; ++i) {
            var color = colors[i];
            var colorCards = MJ.getColorCards(cards, color);
            //如果颜色是宝牌颜色，去除宝牌再检测
            if (color == MJ.getColor(room.baoCard)) {
                    colorCards[room.baoCard] = benCard;

            }
            var colorCardsNum = MJ.sumCardsNum(colorCards);
            if (colorCardsNum == 0) {
                continue;
            }
            var context = {
                result: false,
                hu3x2:hu3x2,
                curbaocard:curBaocard
            };
            if (MJ.isBasicColor(color)) {
                this.checkBaoColorCards(MJ.getValue(color, 1), MJ.getValue(color, MJ.COLOR_CARD_NUM), colorCards, context);
            } else {
                this.checkBaoPairColorCards(colorCards, context);
            }
            if(!context.result){
                pattern = MJ.Pattern.NONE;
                break;
            }
            hu3x2 = context.hu3x2;
            curBaocard = context.curbaocard;
        }
        return pattern;
    },

    isBaohu:function(){
       return this._isBaoHu;
    },
    checkBaoColorCards: function(minCard, maxCard, cards, context){
        var colorCardsNum = MJ.sumCardsNum(cards);
        for(var bn = 0; bn <= context.curbaocard; bn++){
            var curbn = bn;
            var curmodNum = (colorCardsNum + curbn) % 3;
            if(curmodNum == 0){
                this.checkBaoHu3X(minCard, maxCard, cards, context, curbn);
                if (context.result) {
                    context.curbaocard -= curbn;
                    break;
                }
            } else if(curmodNum == 2 && !context.hu3x2){
                this.checkBaoHu3X2(minCard, maxCard, cards, context, curbn);
                if (context.result) {
                    context.curbaocard -= curbn;
                    context.hu3x2 = true;
                    break;
                }
            }
        }
    },
    checkBaoPairColorCards: function(cards, context) {
        var colorCardsNum = MJ.sumCardsNum(cards);
        for(var bn = 0; context.curbaocard >= bn; bn++){
            var curbn = bn;
            var curmodNum = (colorCardsNum + curbn) % 3;
            if(curmodNum == 0){
                context.result = this.checkBaoPairHuColorCards(cards, curbn);
                if (context.result) {
                    context.curbaocard -= curbn;
                    break;
                }
            } else if(curmodNum == 2 && !context.hu3x2){
                for (var i = 1; i <= 7; ++i) {
                    var num = cards[i];
                    curbn = context.curbaocard;
                    if (num >= 2) {
                        var newCards = clone(cards);
                        newCards[i] = num - 2;
                        context.result = this.checkBaoPairHuColorCards(newCards, curbn);
                        if (context.result) {
                            break;
                        }
                    } else if(num == 1 && context.curbaocard) {
                        var newCards = clone(cards);
                        newCards[i] = 0;
                        curbn--;
                        context.result = this.checkBaoPairHuColorCards(newCards, curbn);
                        if (context.result) {
                            break;
                        }
                    }
                }
                if (context.result) {
                    context.curbaocard -= curbn;
                    context.hu3x2 = true;
                    break;
                }
            }
            if(context.result) {
                break;
            }
        }
    },
    checkBaoHu3X: function(minCard, maxCard, cards, context, baocard) {
        var room = this.getRoom();
        var colorCardsNum = MJ.sumCardsNum(cards);
        if (colorCardsNum < 1) {
            context.result = true;
            return;
        }
        for (var i = minCard; i <= maxCard; ++i) {
            var num = cards[i];

            if (!num) {
                continue;
            }
            var num2 = cards[i + 1];
            var num3 = cards[i + 2];
            if (num == 3) {
                if (num2 > 0 && num3 > 0) {
                    var iTmp = 0;
                    if (num2 < 3) {
                        iTmp += 3 - num2;
                    }
                    if (num3 < 3) {
                        iTmp += 3 - num3;
                    }
                    if (iTmp <= baocard) {
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[i] = 0;
                        if (num2 >= 3) {
                            newCards[i + 1] = num2 - 3;
                        } else{
                            newbaocard -= 3 - newCards[i + 1];
                            newCards[i + 1] = 0;
                        }
                        if (num3 >= 3) {
                            newCards[i + 2] = num3 - 3;
                        } else {
                            newbaocard -= 3 - newCards[i + 2];
                            newCards[i + 2] = 0;
                        }
                        this.checkBaoHu3X(minCard, maxCard, newCards, context, newbaocard);
                    }
                }
                cards[i] = 0;
                this.checkBaoHu3X(minCard, maxCard, cards, context, baocard);
            } else if(num == 2){
                if (num2 > 0 && num3 > 0){
                    var iTmp = 0;
                    if (num2 < 2) {
                        iTmp += 2 - num2;
                    }
                    if (num3 < 2) {
                        iTmp += 2 - num3;
                    }
                    if (iTmp <= baocard) {
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[i] = 0;
                        if (num2 >= 2) {
                            newCards[i + 1] = num2 - 2;
                        } else{
                            newbaocard -= 2 - newCards[i + 1];
                            newCards[i + 1] = 0;
                        }
                        if (num3 >= 2) {
                            newCards[i + 2] = num3 - 2;
                        } else {
                            newbaocard -= 2 - newCards[i + 2];
                            newCards[i + 2] = 0;
                        }
                        this.checkBaoHu3X(minCard, maxCard, newCards, context, newbaocard);
                    }
                }
                if(baocard){
                    cards[i] = 0;
                    baocard--;
                    this.checkBaoHu3X(minCard, maxCard, cards, context, baocard);
                }
                if (((num2 || num3) && baocard) || (num2 && num3)) {
                    var newCards = clone(cards);
                    var newbaocard = baocard;
                    newCards[i] = num - 1;
                    if(num2) {
                        newCards[i + 1] = num2 - 1;
                    }else{
                        newbaocard--;
                    }
                    if(num3){
                        newCards[i + 2] = num3 - 1;
                    }
                    else{
                        newbaocard--;
                    }
                    this.checkBaoHu3X(minCard, maxCard, newCards, context, newbaocard);
                }
                if(num == 1 && baocard >= 2){
                    cards[i] = 0;
                    baocard -= 2;
                    this.checkBaoHu3X(minCard, maxCard, cards, context, baocard);
                }
            } else {
                if (((num2 || num3) && baocard) || (num2 && num3)) {
                    var newCards = clone(cards);
                    var newbaocard = baocard;
                    newCards[i] = num - 1;
                    if(num2) {
                        newCards[i + 1] = num2 - 1;
                    }else{
                        newbaocard--;
                    }
                    if(num3){
                        newCards[i + 2] = num3 - 1;
                    }
                    else{
                        newbaocard--;
                    }
                    this.checkBaoHu3X(minCard, maxCard, newCards, context, newbaocard);
                }
                if(num && baocard >= 2){
                    cards[i] -= 1;
                    baocard -= 2;
                    this.checkBaoHu3X(minCard, maxCard, cards, context, baocard);
                }
            }
            break;
        }
    },

    checkBaoHu3X2: function(minCard, maxCard, cards, context, baocard) {
        var room = this.getRoom();
        for (var i = minCard; i <= maxCard; ++i) {
            var num = cards[i];
            if (num >= 2) {
                var newCards = clone(cards);
                newCards[i] = num - 2;
                var newBaocard = baocard;
                this.checkBaoHu3X(minCard, maxCard, newCards, context, newBaocard);
                if (context.result){
                    context.result = true;
                    break;
                }
            } else if(num == 1 && baocard) {
                var newCards = clone(cards);
                newCards[i] = 0;
                var newBaocard = baocard;
                newBaocard--;
                this.checkBaoHu3X(minCard, maxCard, newCards, context, newBaocard);
                if (context.result){
                    context.result = true;
                    break;
                }
            }
        }
    },
    checkBaoPairHuColorCards: function(cards, baocard) {
        var num4 = 0;
        var num3 = 0;
        var num2 = 0;
        var numOther = 0;
        var colorCardsNum = MJ.sumCardsNum(cards);
        if (colorCardsNum < 1) {
            return true;
        }
        var newcard = baocard;
        ExObject.each(cards, function(c, n) {
            if(n == 1){
                if(newcard) {
                    newcard--;
                    num2++;
                }else{
                    numOther++;
                }
            }else if (n == 2) {
                if(newcard) {
                    newcard--;
                    num3++;
                }else{
                    num2++;
                }
            } else if (n == 3) {
                num3++;
            }else if (n == 0) {
            } else {
                numOther++;
            }
        });
        if((numOther == 0) && (num2 <= 1)) {
            return true;
        }
        var context = {result: false};
        this.checkZiCard(cards, context, baocard);
        return context.result;
    },
    checkZiCard:function(cards, context, baocard) {
        var room = this.getRoom();
        var colorCardsNum = MJ.sumCardsNum(cards);
        if (colorCardsNum < 1) {
            context.result = true;
            return;
        }
        var color = MJ.Color.ZI;

        for (var p = 1; p <= 7; ++p) {
            var num = cards[p];
            if (!num) {
                continue;
            }
            var point = p;
            if (point > MJ.ZI.BF) {
                var num2 = 0;
                var num3 = 0;
                var point2 = 0;
                var point3 = 0;
                for (var i = MJ.ZI.HZ; i <= MJ.ZI.BB; i++) {
                    if (i == point) {
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /*if (tmpCard == room.baoCard) {
                        continue
                    }*/
                    num2 = cards[tmpCard];
                    if(!num2){
                        num2 = 0;
                    }
                    point2 = i;
                }
                for (var i = MJ.ZI.HZ; i <= MJ.ZI.BB; i++) {
                    if (i == point) {
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /*if (tmpCard == room.baoCard) {
                        continue
                    }*/
                    if (i == point2) {
                        continue;
                    }
                    num3 = cards[tmpCard];
                    if(!num3){
                        num3 = 0;
                    }
                    point3 = i;
                }
                if (num == 3) {
                    if (num2 > 0 && num3 > 0) {
                        var iTmp = 0;
                        if (num2 < 3) {
                            iTmp += 3 - num2;
                        }
                        if (num3 < 3) {
                            iTmp += 3 - num3;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num2 >= 3) {
                                newCards[point2] = num2 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point2];
                                newCards[point2] = 0;
                            }
                            if (num3 >= 3) {
                                newCards[point3] = num3 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point3];
                                newCards[point3] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    cards[point] = 0;
                    this.checkZiCard(cards, context, baocard);
                } else if (num == 2) {
                    if (num2 > 0 && num3 > 0) {
                        var iTmp = 0;
                        if (num2 < 2) {
                            iTmp += 2 - num2;
                        }
                        if (num3 < 2) {
                            iTmp += 2 - num3;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num2 >= 2) {
                                newCards[point2] = num2 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point2];
                                newCards[point2] = 0;
                            }
                            if (num3 >= 2) {
                                newCards[point3] = num3 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point3];
                                newCards[point3] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if(baocard){
                        cards[point] = 0;
                        baocard--;
                        this.checkZiCard(cards, context, baocard);

                    }
                    if(num2 && num3){
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[point] = num - 1;
                        if (num2) {
                            newCards[point2] = num2 - 1;
                        }
                        if (num3) {
                            newCards[point3] = num3 - 1;
                        }
                        this.checkZiCard(newCards, context, newbaocard);
                    }
                    if(baocard > 0){
                        if(num2 || num3){
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num2) {
                                newCards[point2] = num2 - 1;
                            } else {
                                newbaocard--;
                            }
                            if (num3) {
                                newCards[point3] = num3 - 1;
                            }
                            else {
                                newbaocard--;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if (num == 1 && baocard >= 2) {
                        cards[point] = 0;
                        baocard -= 2;
                        this.checkZiCard(cards, context, baocard);
                    }
                } else {
                    if(num2 && num3){
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[point] = num - 1;
                        if (num2) {
                            newCards[point2] = num2 - 1;
                        }
                        if (num3) {
                            newCards[point3] = num3 - 1;
                        }
                        this.checkZiCard(newCards, context, newbaocard);
                    }
                    if(baocard > 0){
                        if(num2 || num3){
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num2) {
                                newCards[point2] = num2 - 1;
                            } else {
                                newbaocard--;
                            }
                            if (num3) {
                                newCards[point3] = num3 - 1;
                            }
                            else {
                                newbaocard--;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if (num == 1 && baocard >= 2) {
                        cards[point] = 0;
                        baocard -= 2;
                        this.checkZiCard(cards, context, baocard);
                    }
                }
                break;
            } else {
                var num2 = 0;
                var num3 = 0;
                var num4 = 0;
                var point2 = 0;
                var point3 = 0;
                var point4 = 0;
                for (var i = MJ.ZI.DF; i <= MJ.ZI.BF; i++) {
                    if (i == point) {
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /*if (tmpCard == room.baoCard) {
                        continue
                    }*/
                    num2 = cards[tmpCard];
                    if(!num2){
                        num2 = 0;
                    }
                    point2 = i;
                }
                for (var i = MJ.ZI.DF; i <= MJ.ZI.BF; i++) {
                    if (i == point) {
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /*if (tmpCard == room.baoCard) {
                        continue
                    }*/
                    if (i == point2) {
                        continue;
                    }
                    num3 = cards[tmpCard];
                    if(!num3){
                        num3 = 0;
                    }
                    point3 = i;
                }
                for (var i = MJ.ZI.DF; i <= MJ.ZI.BF; i++) {
                    if (i == point) {
                        continue;
                    }
                    var tmpCard = MJ.getValue(color, i);
                    /*if (tmpCard == room.baoCard) {
                        continue
                    }*/
                    if (i == point2) {
                        continue;
                    }
                    if (i == point3) {
                        continue;
                    }
                    num4 = cards[tmpCard];
                    if(!num4){
                        num4 = 0;
                    }
                    point4 = i;
                }
                if (num == 3) {
                    if (num2 > 0 && num3 > 0 && !context.result) {
                        var iTmp = 0;
                        if (num2 < 3) {
                            iTmp += 3 - num2;
                        }
                        if (num3 < 3) {
                            iTmp += 3 - num3;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num2 >= 3) {
                                newCards[point2] = num2 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point2];
                                newCards[point2] = 0;
                            }
                            if (num3 >= 3) {
                                newCards[point3] = num3 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point3];
                                newCards[point3] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if(num2 > 0 && num4 > 0 && !context.result) {
                        var iTmp = 0;
                        if (num2 < 3) {
                            iTmp += 3 - num2;
                        }
                        if (num3 < 3) {
                            iTmp += 3 - num4;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num2 >= 3) {
                                newCards[point2] = num2 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point2];
                                newCards[point2] = 0;
                            }
                            if (num4 >= 3) {
                                newCards[point4] = num4 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point4];
                                newCards[point4] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if(num3 > 0 && num4 > 0 && !context.result) {
                        var iTmp = 0;
                        if (num3 < 3) {
                            iTmp += 3 - num3;
                        }
                        if (num4 < 3) {
                            iTmp += 3 - num4;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num3 >= 3) {
                                newCards[point3] = num3 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point3];
                                newCards[point3] = 0;
                            }
                            if (num4 >= 3) {
                                newCards[point4] = num4 - 3;
                            } else {
                                newbaocard -= 3 - newCards[point4];
                                newCards[point4] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    cards[point] = 0;
                    this.checkZiCard(cards, context, baocard);
                } else if (num == 2) {
                    if (num2 > 0 && num3 > 0) {
                        var iTmp = 0;
                        if (num2 < 2) {
                            iTmp += 2 - num2;
                        }
                        if (num3 < 2) {
                            iTmp += 2 - num3;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num2 >= 2) {
                                newCards[point2] = num2 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point2];
                                newCards[point2] = 0;
                            }
                            if (num3 >= 2) {
                                newCards[point3] = num3 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point3];
                                newCards[point3] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                        if(num2 && num3){
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num2) {
                                newCards[point2] = num2 - 1;
                            }
                            if (num3) {
                                newCards[point3] = num3 - 1;
                            }

                            this.checkZiCard(newCards, context, newbaocard);
                        }
                        if(baocard > 0){
                            if (num2 || num3) {
                                var newCards = clone(cards);
                                var newbaocard = baocard;
                                newCards[point] = num - 1;
                                if (num2) {
                                    newCards[point2] = num2 - 1;
                                } else {
                                    newbaocard--;
                                }
                                if (num3) {
                                    newCards[point3] = num3 - 1;
                                }
                                else {
                                    newbaocard--;
                                }
                                this.checkZiCard(newCards, context, newbaocard);
                            }
                        }
                    }
                    if(num2 > 0 && num4 > 0 && !context.result) {
                        var iTmp = 0;
                        if (num2 < 2) {
                            iTmp += 2 - num2;
                        }
                        if (num3 < 2) {
                            iTmp += 2 - num4;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num2 >= 2) {
                                newCards[point2] = num2 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point2];
                                newCards[point2] = 0;
                            }
                            if (num4 >= 2) {
                                newCards[point4] = num4 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point4];
                                newCards[point4] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }

                        if(num2 && num4){
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num2) {
                                newCards[point2] = num2 - 1;
                            }
                            if (num4) {
                                newCards[point4] = num4 - 1;
                            }

                            this.checkZiCard(newCards, context, newbaocard);
                        }
                        if(baocard > 0){
                            if (num2 || num4) {
                                var newCards = clone(cards);
                                var newbaocard = baocard;
                                newCards[point] = num - 1;
                                if (num2) {
                                    newCards[point2] = num2 - 1;
                                } else {
                                    newbaocard--;
                                }
                                if (num4) {
                                    newCards[point4] = num4 - 1;
                                }
                                else {
                                    newbaocard--;
                                }
                                this.checkZiCard(newCards, context, newbaocard);
                            }
                        }
                    }
                    if(num3 > 0 && num4 > 0 && !context.result) {
                        var iTmp = 0;
                        if (num3 < 2) {
                            iTmp += 2 - num3;
                        }
                        if (num4 < 2) {
                            iTmp += 2 - num4;
                        }
                        if (iTmp <= baocard) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = 0;
                            if (num3 >= 2) {
                                newCards[point3] = num3 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point3];
                                newCards[point3] = 0;
                            }
                            if (num4 >= 2) {
                                newCards[point4] = num4 - 2;
                            } else {
                                newbaocard -= 2 - newCards[point4];
                                newCards[point4] = 0;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                        if(num4 && num3){
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num4) {
                                newCards[point4] = num4 - 1;
                            }
                            if (num3) {
                                newCards[point3] = num3 - 1;
                            }

                            this.checkZiCard(newCards, context, newbaocard);
                        }
                        if(baocard > 0){
                            if (num4 || num3) {
                                var newCards = clone(cards);
                                var newbaocard = baocard;
                                newCards[point] = num - 1;
                                if (num4) {
                                    newCards[point4] = num4 - 1;
                                } else {
                                    newbaocard--;
                                }
                                if (num3) {
                                    newCards[point3] = num3 - 1;
                                }
                                else {
                                    newbaocard--;
                                }
                                this.checkZiCard(newCards, context, newbaocard);
                            }
                        }
                    }
                    if(baocard){
                        cards[point] = 0;
                        baocard--;
                        this.checkZiCard(cards, context, baocard);

                    }

                } else {
                    if(num2 && num3){
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[point] = num - 1;
                        if (num2) {
                            newCards[point2] = num2 - 1;
                        }
                        if (num3) {
                            newCards[point3] = num3 - 1;
                        }

                        this.checkZiCard(newCards, context, newbaocard);
                    }
                    if(baocard > 0){
                        if (num2 || num3) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num2) {
                                newCards[point2] = num2 - 1;
                            } else {
                                newbaocard--;
                            }
                            if (num3) {
                                newCards[point3] = num3 - 1;
                            }
                            else {
                                newbaocard--;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if(num2 && num4){
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[point] = num - 1;
                        if (num2) {
                            newCards[point2] = num2 - 1;
                        }
                        if (num4) {
                            newCards[point4] = num4 - 1;
                        }

                        this.checkZiCard(newCards, context, newbaocard);
                    }
                    if(baocard > 0){
                        if (num2 || num4) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num2) {
                                newCards[point2] = num2 - 1;
                            } else {
                                newbaocard--;
                            }
                            if (num4) {
                                newCards[point4] = num4 - 1;
                            }
                            else {
                                newbaocard--;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                    if(num4 && num3){
                        var newCards = clone(cards);
                        var newbaocard = baocard;
                        newCards[point] = num - 1;
                        if (num4) {
                            newCards[point4] = num4 - 1;
                        }
                        if (num3) {
                            newCards[point3] = num3 - 1;
                        }

                        this.checkZiCard(newCards, context, newbaocard);
                    }
                    if(baocard > 0){
                        if (num4 || num3) {
                            var newCards = clone(cards);
                            var newbaocard = baocard;
                            newCards[point] = num - 1;
                            if (num4) {
                                newCards[point4] = num4 - 1;
                            } else {
                                newbaocard--;
                            }
                            if (num3) {
                                newCards[point3] = num3 - 1;
                            }
                            else {
                                newbaocard--;
                            }
                            this.checkZiCard(newCards, context, newbaocard);
                        }
                    }
                }
                break;
            }
        }
    },
    /**
     * 是否可以胡13幺
     * @param cards
     */
    checkBaoHuOne13: function(cards) {
        var cardArr = Object.keys(cards);
        if (cardArr.length != 13) {
            return false;
        }

        return (cards[MJ.Value.TONG1] > 0) &&
            (cards[MJ.Value.TONG9] > 0) &&
            (cards[MJ.Value.TIAO1] > 0) &&
            (cards[MJ.Value.TIAO9] > 0) &&
            (cards[MJ.Value.WAN1] > 0) &&
            (cards[MJ.Value.WAN9] > 0) &&
            (cards[MJ.ZI.DF] > 0) &&
            (cards[MJ.ZI.NF] > 0) &&
            (cards[MJ.ZI.XF] > 0) &&
            (cards[MJ.ZI.BF] > 0) &&
            (cards[MJ.ZI.HZ] > 0) &&
            (cards[MJ.ZI.FC] > 0) &&
            (cards[MJ.ZI.BB] > 0);
    },

    checkBaoHuPair7: function(cards,baocard) {
        var num = 0;
        var room = this.getRoom();
        ExObject.eachKeyNum(cards, function(c, n) {
            if(c != room.baoCard){
                if (n == 2) {
                    num += 1;
                    cards[c] = 0;
                } else if (n == 4) {
                    num += 2;
                    cards[c] = 0;
                }else  if(n == 3){
                    num += 1;
                    cards[c] -= 2;
                }
            }
        });

        return (baocard >= 7 - num);
    },
    checkBaoHuBigPair: function(cards,baocard) {
        var num = 0;
        var num1 = 0;
        var num2 = 0;
        var num3 = 0;
        var num4 = 0;
        var card = baocard;
        var room = this.getRoom();
        ExObject.eachKeyNum(cards, function(c, n) {
            if(c != room.baoCard){
                if (n == 2) {
                    if(card){
                        card--;
                    }else{
                        num2++;
                    }
                } else if (n == 4) {
                    num4++;
                }else  if(n == 3){
                }else if(n == 1){
                    if(card){
                        card--;
                        num2++;
                    }else{
                        num1++;
                    }
                }
            }
            num++;
        });
        if(num2 == 2 && num1 == 0 && card > 0 && num4 == 0) {
            return true
        }else if(num1 == 1 && num2 == 0 && card > 0 && num4 == 0){
            return true
        }else if(num1 == 0 && num2 == 0 && num4 == 0) {
            return true;
        }else if(num2 == 1 && num1 == 0 && num4 == 0){
            return true;
        }
        return false
    },
    check13BuDa: function(cards, bao, bencard){
        var room = this.getRoom();
        var colors = room.getSubConfig().colors;
        var pattern = MJ.Pattern.NORMAL;
        var CardsNum = MJ.sumCardsNum(cards);
        if (CardsNum < 13) {
            return false;
        }
        if(!bencard){
            bencard = 0;
        }
        if(bao) {
            cards[room.baoCard] = bencard;
        }

        for (var i = 0, l = colors.length; i < l; ++i) {
            var color = colors[i];
            var minCard = MJ.getValue(color, 1);
            var maxCard = MJ.getValue(color, MJ.COLOR_CARD_NUM);
            var colorCards = MJ.getColorCards(cards, color);
            var colorCardsNum = MJ.sumCardsNum(colorCards);
            if (colorCardsNum < 1) {
               continue;
            }
            if (MJ.isBasicColor(color)) {
                if(!this.checkColorBuDa(minCard, maxCard, colorCards)){
                    return false;
                }
            } else {
                var num = true;
                ExObject.each(colorCards, function(c, n) {
                    if (n > 1) {
                        num = false;
                    }
                });
                if(!num){
                    return false;
                }
            }
        }
        return true;
    },
    checkColorBuDa:function(minCard, maxCard ,cards){
        for (var i = minCard; i <= maxCard; ++i) {
            var num = cards[i];
            if (!num) {
                continue;
            }
            if(num != 1){
                return false;
            }
            var num2 = cards[i + 1];
            var num3 = cards[i + 2];
            if(num2 || num3){
                return false;
            }
        }
        return true;
    },
});